package com.wecloud.core.server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.wecloud.core.constant.CorePathConfig;
import com.wecloud.core.constant.ErrorMsg;
import com.wecloud.core.constant.WeContext;
import com.wecloud.core.exception.WeException;
@Component
public class UploadFilesServer {
//	public static final String URL = "/file/";
	Logger log = LoggerFactory.getLogger(UploadFilesServer.class);
	@Async("uploadAsync")
	public Future execute(WeContext context,MultipartHttpServletRequest req) throws UnsupportedEncodingException {
		context.getRequest().setCharacterEncoding("UTF-8");
		List<MultipartFile> files = req.getFiles("file");
		String path = "" ;
		path = java.net.URLDecoder.decode(CorePathConfig.ROOT_PATH + context.getUser().getUserId() + CorePathConfig.USER_ROOT_PATH, "UTF-8");//文件存放路径
//		if("saysomething".equals(context.getTransactionId())){
//			path = java.net.URLDecoder.decode(CorePathConfig.ROOT_PATH  + context.getUser().getUserId() + CorePathConfig.USER_SS_PATH, "UTF-8");//文件存放路径
//		}
		if("updateTx".equals(context.getTransactionId())){
			path = java.net.URLDecoder.decode(CorePathConfig.ROOT_PATH  + context.getUser().getUserId() + CorePathConfig.USER_TX_PATH, "UTF-8");//文件存放路径
		}
		FileOutputStream fileOutput = null;
		List list = new ArrayList();
		if(files != null && files.size() > 0){
			for (MultipartFile file : files) {
				if (!new File(path).exists()) {
					new File(path).mkdirs();
				}
				if(new File(path + file.getOriginalFilename()).exists() && !"saysomething".equals(context.getTransactionId()) && !"updateTx".equals(context.getTransactionId())){
					context.setData("exists", file.getOriginalFilename()+"已存在");
					continue;
				}
				try {
					fileOutput = new FileOutputStream(path + file.getOriginalFilename());
					log.error("#######################################"+file.getOriginalFilename()+"##########################");
					fileOutput.write(file.getBytes());
					fileOutput.flush();
					Map map = new HashMap();
					map.put("filename", file.getOriginalFilename());
					map.put("path", path.replace(CorePathConfig.ROOT_PATH , ""));
					if(file.getOriginalFilename().endsWith("doc")||file.getOriginalFilename().endsWith("docx")){
						map.put("fileType", "word");
					}
					if(file.getOriginalFilename().endsWith("cls")||file.getOriginalFilename().endsWith("xcls")){
						map.put("fileType", "excel");
					}
					if(file.getOriginalFilename().endsWith("mp3")||file.getOriginalFilename().endsWith("amr")){
						map.put("fileType", "mp3");
					}
					if(file.getOriginalFilename().endsWith("mp4")||file.getOriginalFilename().endsWith("avi")
							||file.getOriginalFilename().endsWith("rmvb") ||file.getOriginalFilename().endsWith("rm")){
						map.put("fileType", "videos");
					}
					if(file.getOriginalFilename().toUpperCase().endsWith("JPEG")||file.getOriginalFilename().toUpperCase().endsWith("GIF")
							||file.getOriginalFilename().toUpperCase().endsWith("JPG")){
						map.put("fileType", "photos");
					}
					if(file.getOriginalFilename().toUpperCase().endsWith("ZIP")||file.getOriginalFilename().toUpperCase().endsWith("RAR")
							||file.getOriginalFilename().toUpperCase().endsWith("7Z")||file.getOriginalFilename().toUpperCase().endsWith("JAR")){
						map.put("fileType", "ZIPS");
					}
					list.add(map);
				} catch (FileNotFoundException e) {
					throw new WeException(ErrorMsg.Error_9999);
				} catch (IOException e) {
					throw new WeException(ErrorMsg.Error_9999);
				}finally{
					try {
						fileOutput.close();
					} catch (IOException e) {
						throw new WeException(ErrorMsg.Error_9999);
					}
				}
			}
		}
		return new AsyncResult(list);
	}
}
